using System;
using System.Runtime.InteropServices;
using System.Security.Permissions;
using Microsoft.SharePoint;
using System.Linq;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint.WebPartPages;
using System.Collections.Generic;
using System.IO;
using SlamCms.SharePoint.Publishing.WebParts;
using SlamCms.SharePoint.WebParts;
using System.Xml;
using SlamCms.SharePoint.Core;

namespace SlamCms.SharePoint.Features.PublishingHomeMain
{
	[Guid("cf6911e8-4a67-423b-ab23-c76c5c319e34")]
	public class PublishingHomeMainEventReceiver : SPFeatureReceiver
	{
		public override void FeatureActivated(SPFeatureReceiverProperties properties)
		{
			base.FeatureActivated(properties);

			string layoutPagePath = Path.Combine(properties.Definition.RootDirectory, "PublishingHomeMain\\default.aspx");

			FileInfo layoutPageFile = new FileInfo(layoutPagePath);
			if(layoutPageFile.Exists)
			{
				SPWeb web = (SPWeb)properties.Feature.Parent;
				SlamCmsContext.SharePoint.WithElevatedPrivileges().SiteCollection(web.Site.ID).Site(web.ID,	elevatedWeb =>
					{
						using (FileStream fileStream = new FileStream(layoutPagePath, FileMode.Open))
						{
							elevatedWeb.RootFolder.Files.Add("default.aspx", fileStream, true);
							elevatedWeb.RootFolder.Update();
						}

						ClearWebParts(elevatedWeb);

						using (SPLimitedWebPartManager webPartManager = elevatedWeb.GetLimitedWebPartManager(web.RootFolder.ServerRelativeUrl.TrimEnd('/') + "/default.aspx", PersonalizationScope.Shared))
						{
							PublishingSitesLinks publishingSiteLinksWebPart = new PublishingSitesLinks();
							publishingSiteLinksWebPart.Title = "Publishing Sites";
							publishingSiteLinksWebPart.AllowClose = false;
							publishingSiteLinksWebPart.AllowEdit = false;
							publishingSiteLinksWebPart.AllowHide = false;
							publishingSiteLinksWebPart.AllowMinimize = false;
							publishingSiteLinksWebPart.AllowZoneChange = false;
							webPartManager.AddWebPart(publishingSiteLinksWebPart, "Left", 0);

							ExpiringContent expiringContentWebPart = new ExpiringContent();
							expiringContentWebPart.Title = "Expiring Content";
							expiringContentWebPart.PageSize = 10;
							expiringContentWebPart.AllowClose = false;
							expiringContentWebPart.AllowEdit = false;
							expiringContentWebPart.AllowHide = false;
							expiringContentWebPart.AllowMinimize = false;
							expiringContentWebPart.AllowZoneChange = false;
							webPartManager.AddWebPart(expiringContentWebPart, "Center", 0);

							PublishingContent draftPublishingContentWebPart = new PublishingContent();
							draftPublishingContentWebPart.Title = "Draft Content";
							draftPublishingContentWebPart.PageSize = 10;
							draftPublishingContentWebPart.ContentStatus = PublishingContentStatus.Draft;
							draftPublishingContentWebPart.AllowClose = false;
							draftPublishingContentWebPart.AllowEdit = false;
							draftPublishingContentWebPart.AllowHide = false;
							draftPublishingContentWebPart.AllowMinimize = false;
							draftPublishingContentWebPart.AllowZoneChange = false;
							webPartManager.AddWebPart(draftPublishingContentWebPart, "Center", 1);

							PublishingContent publishedPublishingContentWebPart = new PublishingContent();
							publishedPublishingContentWebPart.Title = "Recently Published";
							publishedPublishingContentWebPart.PageSize = 10;
							publishedPublishingContentWebPart.ContentStatus = PublishingContentStatus.Published;
							publishedPublishingContentWebPart.AllowClose = false;
							publishedPublishingContentWebPart.AllowEdit = false;
							publishedPublishingContentWebPart.AllowHide = false;
							publishedPublishingContentWebPart.AllowMinimize = false;
							publishedPublishingContentWebPart.AllowZoneChange = false;
							webPartManager.AddWebPart(publishedPublishingContentWebPart, "Center", 2);

							VersionCheck versionCheckWebPart = new VersionCheck();
							versionCheckWebPart.Title = "Version Check";
							versionCheckWebPart.ChromeType = PartChromeType.None;
							versionCheckWebPart.AllowClose = false;
							versionCheckWebPart.AllowEdit = false;
							versionCheckWebPart.AllowHide = false;
							versionCheckWebPart.AllowMinimize = false;
							versionCheckWebPart.AllowZoneChange = false;
							webPartManager.AddWebPart(versionCheckWebPart, "Right", 0);

							webPartManager.Web.Dispose();
						}
					});
			}
		}

		public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
		{
			base.FeatureDeactivating(properties);

			SPWeb web = (SPWeb)properties.Feature.Parent;
			ClearWebParts(web);
		}

		private void ClearWebParts(SPWeb web)
		{
			using (SPLimitedWebPartManager webPartManager = web.GetLimitedWebPartManager(web.RootFolder.ServerRelativeUrl.TrimEnd('/') + "/default.aspx", PersonalizationScope.Shared))
			{
				List<System.Web.UI.WebControls.WebParts.WebPart> webPartsToDelete = new List<System.Web.UI.WebControls.WebParts.WebPart>();
				foreach (System.Web.UI.WebControls.WebParts.WebPart webPart in webPartManager.WebParts)
				{
					webPartsToDelete.Add(webPart);
				}

				foreach (System.Web.UI.WebControls.WebParts.WebPart webPart in webPartsToDelete)
				{
					webPartManager.DeleteWebPart(webPart);
				}

				webPartManager.Web.Dispose();
			}
		}
	}
}
